<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="UTF-8">
    <title>登录中...</title>
    <script src="../o2_core/o2.min.js"></script>
    <script src="../o2_lib/Decimal.js"></script>
    <script>
        function getServiceAddress(config, callback){
            if (typeOf(config.center)=="object"){
                getServiceAddressConfigObject(callback);
            }else if (typeOf(config.center)=="array"){
                var center = chooseCenter(config);
                if (center){
                    getServiceAddressConfigObject(callback, center);
                }else{
                    getServiceAddressConfigArray(config, callback);
                }
                //this.getServiceAddressConfigArray(callback);
            }
        }
        function chooseCenter(config){
            var host = window.location.host;
            var center = null;
            for (var i=0; i<config.center.length; i++){
                var ct = config.center[i];
                if (ct.webHost==host){
                    center = ct;
                    break;
                }
            }
            return center;
        }
        function getServiceAddressConfigArray(config, callback) {
            var requests = [];
            config.center.each(function(center){
                requests.push(
                        getServiceAddressConfigObject(function(address){
                            requests.each(function(res){
                                if (res.isRunning()){res.cancel();}
                            });
                            if (callback) callback(address);
                        }.bind(this), center)
                );
            }.bind(this));
        }
        function getServiceAddressConfigObject(callback, center){
            var centerConfig = center;
            var host = centerConfig.host || window.location.hostname;
            var port = centerConfig.port;
            var uri = "";
            var locate = window.location;
            var protocol = locate.protocol;
            if (!port || port=="80"){
                uri = protocol+"//"+host+"/x_program_center/jaxrs/distribute/assemble/source/{source}";
            }else{
                uri = protocol+"//"+host+":"+port+"/x_program_center/jaxrs/distribute/assemble/source/{source}";
            }
            var currenthost = window.location.hostname;
            uri = uri.replace(/{source}/g, currenthost);
            //var uri = "http://"+layout.config.center+"/x_program_center/jaxrs/distribute/assemble";
            return MWF.restful("get", uri, null, function(json){
                var serviceAddressList = json.data;
                var addressObj = serviceAddressList["x_organization_assemble_authentication"];
                var address = protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+"/z_sso_control/jaxrs/sso/smplogin";

                if (callback) callback(address);
            }.bind(this));
        }

        o2.addReady(function(){
            o2.JSON.get("res/config/config.json", function(config){
                window.layout = {};
                window.layout.config = config;
                getServiceAddress(config, function(address){
                    var uri = new URI(window.location.toString());
                    var xtoken = uri.getData("SMAP_SESSION_DATA");
                    var appID = uri.getData("appID");
                    var uniqueId = uri.getData("uniqueId");
                    var redirect = uri.getData("redirect");
                    redirect = redirect + "&uniqueId=" + uniqueId;
                    if (xtoken){
                        var res = new Request.JSON({
                            url: o2.filterUrl(address+"?SMAP_SESSION_DATA="+encodeURIComponent(xtoken)+"&appID="+appID+"&date="+(new Date()).getTime()),
                            secure: false,
                            method: "GET",
                            noCache: true,
                            withCredentials: true,
                            onSuccess: function(responseJSON, responseText){
                                var result = responseJSON.data.value;
								if(result){
									if (redirect){
										window.location = redirect;
									}else{
										window.location = "/";
									}
								}else{
									$("ssoerror").set("html","单点失败！"+responseJSON.message);
									$("ssoerror").show();
								}
                            }.bind(this),
                            onFailure: function(xhr){
                                $("ssoerror").show();
                            }.bind(this),
                            onError: function(text, error){
                                $("ssoerror").show();
                            }.bind(this)
                        });

                        res.send();
                    }else{
						$("ssoerror").set("html","单点失败，认证token为空！");
                        $("ssoerror").show();
                    }
                });
            });

        });
    </script>
</head>
<body style="background: rgb(239, 239, 244);">
<div id="ssoerror" style="text-align:center;display:none;">单点失败，请联系管理员！</div>
</body>
</html>
